/**
 *  Route definitions for all exposed API interfaces
 *
 *  @author Scott Monahan
 *  @version $Id$
 *  
 *  File last changed on $Date:$ by $Author:$ in $Rev:$
 *
 */

package info637.pprc.routes;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
import org.springframework.beans.factory.annotation.Value;

public class Routes extends RouteBuilder {

	@Value("${restURL}")
	private String restURL = null;
	
	@Value("${version}")
	private String version = null;

	@Override
	public void configure() throws Exception {
		
		log.info("Starting PPRC Server - Version: " + this.version);

		onException(Exception.class).handled(true).process(new Processor() {

			@Override
			public void process(Exchange exchange) throws Exception {

				Exception e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
						Exception.class);

				exchange.getIn().setBody("Error: " + e.getMessage());
			}
		});
		
		from(restURL)
			.choice()
				.when().simple("${in.header.CamelHttpPath} contains 'admin'")
					.beanRef("adminRESTAdaptor")
				.when().simple("${in.header.CamelHttpPath} contains 'public'")
					.beanRef("publicRESTAdaptor")
			.end()
		.marshal()
		.json(JsonLibrary.Jackson);
		
		from("quartz://session/timeout?trigger.repeatInterval=15000&trigger.repeatCount=-1")
			.beanRef("sessionService", "checkForTimeouts");
	}
}